<?php

class SiteController extends Controller
{
    protected $mc;
    public $page;
    public function init()
    {
        try {
            // $this->mc = new Mailchimp('9cef453a982a4c1f0c8b0e0eebe7a983-us3'); //your api key here
            $this->mc = new Mailchimp('3e3bc7211c1d284592886a43d00398eb-us5'); //your api key here
        } catch (Mailchimp_Error $e) {
            throw new Exception("You have not set an API key. Please set it in SiteController");
        }
    }

    public function actionIndex()
    {
        $this->redirect(array('subscribe'));
    }

    public function actionList()
    {
        try {
            $lists = $this->mc->lists->getList();
        } catch (Mailchimp_Invalid_ApiKey $e) {
            Yii::app()->user->setFlash('error', 'The API key is invalid. Change it in Controller/AppController.php');
        } catch (Mailchimp_Error $e) {
            if ($e->getMessage()) {
                Yii::app()->user->setFlash('error', $e->getMessage());
            } else {
                Yii::app()->user->setFlash('error', 'An unknown error occurred');
            }
        }
        $this->render('list',array('lists'=>$lists));
    }

    public function actionListView($id)
    {
        try {
            $lists = $this->mc->lists->getList(array('list_id'=>$id));
            $list = $lists['data'][0];
            $member_res = $this->mc->lists->members($list['id']);
            $this->render('listView', array('list'=>$list, 'members'=>$member_res['data']));
        } catch (Mailchimp_List_DoesNotExist $e) {
            Yii::app()->user->setFlash('error', 'The list does not exist');
            $this->redirect(array('list'));
        } catch (Mailchimp_Error $e) {
            if ($e->getMessage()) {
                Yii::app()->user->setFlash('error', $e->getMessage());
            } else {
                Yii::app()->user->setFlash('error', 'An unknown error occurred');
            }
            $this->redirect(array('list'));
        }
    }

    public function actionListSubscribe($id)
    {
        $email = Yii::app()->request->getParam('email');
        try {
            $this->mc->lists->subscribe($id, array('email'=>$email));
            Yii::app()->user->setFlash('success', 'User subscribed successfully!');
        } catch (Mailchimp_Error $e) {
            if ($e->getMessage()) {
                Yii::app()->user->setFlash('error', $e->getMessage());
            } else {
                Yii::app()->user->setFlash('error', 'An unknown error occurred');
            }
        }
        $this->redirect(array('list','id'=>$id));
    }

    public function actionDK()
    {
        $id = '2a8aea1a9f';
        $email = 'anhkhoa@happyliving.vn';

        try {
            $this->mc->lists->subscribe($id, array('email'=>$email));
            Yii::app()->user->setFlash('success', 'User subscribed successfully!');
        } catch (Mailchimp_Error $e) {
            if ($e->getMessage()) {
                Yii::app()->user->setFlash('error', $e->getMessage());
            } else {
                Yii::app()->user->setFlash('error', 'An unknown error occurred');
            }
        }
        $this->redirect(array('index'));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     *
    */
    public function actionSubscribe($ref_id=false)
    {
        $this->page='subscribe';
        $gioithieu_id = 0;

        if($ref_id)
        {
            $gioithieu = User::model()->findByPk((int)$ref_id);
            if($gioithieu !== NULL) $gioithieu_id = $gioithieu->id;
        }

        $this->render('subscribe', array('gioithieu_id'=>$gioithieu_id));
    }

    public function actionActiveView($activate_code)
    {
        $this->render('activeView', array('activate_code'=>$activate_code));
    }

    public function actionActivate($activate_code)
    {
        $criteria = new CDbCriteria;
        $criteria->condition = 'activate_code=:activate_code';
        $criteria->params = array(':activate_code'=>$activate_code);
        $user = User::model()->find($criteria);
        
        if($user===NULL) {
            Yii::app()->user->setFlash('error', 'Invalid activation code.');
        } else {
            $user->status = User::ACTIVE;
            $user->update_time = time();
            $user->update(array('status', 'update_time'));
            $message = 'Activated successfully. Share link: ' . $this->createAbsoluteURL('subscribe',array('ref_id'=>$user->id));
            Yii::app()->user->setFlash('success', $message);
        }

        $this->render('activate');
    }

    public function actionComplete()
    {
        $this->page = 'complete';
        $this->render('complete');
    }

    public function actionHook()
    {
        $result = file_get_contents('http://requestb.in/14zh0ve1');
        echo $result;
        Yii::log('tatda', json_encode($_POST));

    }

    
}